package com.xintong.visualinspection.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mysql.jdbc.StringUtils;
import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.CheckRule;
import com.xintong.visualinspection.service.CheckRuleService;

/**
 * 文件名：TestController
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@RestController
@RequestMapping("/checkRule")
public class CheckRuleController extends BaseController {

    @Autowired
    private CheckRuleService checkRuleService;

   
    /**
     * 添加考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(@Valid @RequestBody CheckRule checkRule){
    	checkRuleService.insert(checkRule);
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 修改考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(@Valid @RequestBody CheckRule checkRule){
    	if(checkRule.getId()==null){
    		throw new BusinessException(20002);
    	}
    	checkRuleService.update(checkRule);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 删除考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody CheckRule checkRule){
    	if(checkRule.getId()==null){
    		throw new BusinessException(20002);
    	}
    	checkRuleService.delete(checkRule.getId());
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 通过id获取考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getById")
    public String getById(@RequestBody CheckRule checkRule){
    	if(checkRule.getId()==null){
    		throw new BusinessException(20002);
    	}
    	CheckRule cr = checkRuleService.getById(checkRule.getId());
    	return super.returnSuccessResult(cr);
    }
    
    /**
     * 通过名称获取考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getByName")
    public String getByName(@RequestBody CheckRule checkRule){
    	if(StringUtils.isNullOrEmpty(checkRule.getName())){
    		throw new BusinessException(21101);
    	}
    	List<CheckRule> crList = checkRuleService.getByName(checkRule.getName());
    	return super.returnSuccessResult(crList);
    }
}
